package com.xbz.common.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.HashSet;
import java.util.Set;


@Configuration
@Profile({"dev", "local"})
@EnableOpenApi
//@EnableSwaggerBootstrapUI
public class SwaggerConfig {


    @Bean
    public Docket createRestApi(Environment environment){

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev1","dev2");

        //获取项目的环境,通过acceptsProfiles判断是否在设定的环境
        boolean flag = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组为base
                .groupName("base")
                .enable(true)
                .select()
                /*
                RequestHandlerSelectors,配置要扫描接口的方式
                basePackage:指定要扫描的包
                any()扫描全部
                none()不扫描
                withClassAnnotation 扫描类上的注解
                */
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //过滤扫描什么路径
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 添加摘要信息
     * @return 返回ApiInfo对象
     */
    private ApiInfo apiInfo() {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                .title("基本接口测试的接口文档")
                .description("相关接口的文档")
                .termsOfServiceUrl("http://localhost:8080/hello")
                .version("1.0")
                .build();
    }
}
